<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class CallBack extends \app\ApiRest
{
    protected $app;
    public function __construct(\think\App $app)
    {
        $this->app = $app;
    }
    public function fddAttestationCallBack()
    {
        $inputs = $_POST;
        $dis = ["customer_id" => $inputs["customerId"], "status" => 1];
        $att_model = new \app\fdd\model\FddAttestationRecord();
        $record = $att_model->dataInfo($dis);
        if(!empty($record)) {
            if($inputs["status"] == 2 && $record["type"] == 1 || $inputs["status"] == 4 && $record["type"] == 2) {
                $update["status"] = 2;
                $core = new \longbingcore\wxcore\Fdd($record["uniacid"]);
                $res = $core->ApplyCert($record["customer_id"], $record["transactionNo"]);
                if(isset($res["code"]) && $res["code"] == 1) {
                    $update["status"] = 3;
                }
            }
            $update["statusDesc"] = $inputs["statusDesc"];
            $update["sign"] = base64_encode($inputs["sign"]);
            $att_model->dataUpdate($dis, $update);
        }
        $res = ["code" => 0, "msg" => "成功"];
        echo json_encode($res);
        exit;
    }
    public function fddSignCallBack()
    {
        $inputs = $_POST;
        $dis = ["contract_id" => $inputs["contract_id"]];
        $model = new \app\fdd\model\FddAgreementRecord();
        $data = $model->dataInfo($dis);
        if(!empty($data)) {
            $update = ["result_code" => $inputs["result_code"], "result_desc" => $inputs["result_desc"], "msg_digest" => base64_decode($inputs["msg_digest"])];
            if($inputs["result_code"] == 3000) {
                $update["status"] = $data["status"] == 1 ? 2 : 3;
            }
            if(!empty($inputs["download_url"])) {
                $update["download_url"] = $inputs["download_url"];
            }
            if(!empty($inputs["viewpdf_url"])) {
                $update["viewpdf_url"] = $inputs["viewpdf_url"];
            }
            $admin_model = new \app\massage\model\Admin();
            if(!empty($data["admin_id"])) {
                $admin = $admin_model->dataInfo(["id" => $data["admin_id"]]);
            } else {
                $admin = $admin_model->dataInfo(["is_admin" => 1]);
            }
            $model->dataUpdate($dis, $update);
            if(!empty($update["status"]) && $update["status"] == 3) {
                $attestation_model = new \app\fdd\model\FddAttestationRecord();
                $res = $attestation_model->ContractFiling($data["user_id"], $data["uniacid"], $data["admin_id"]);
                if(empty($res["code"])) {
                    $year = $admin["agreement_time"];
                    $time = strtotime("+" . $year . " year");
                    $model->dataUpdate(["id" => $data["id"]], ["status" => 4, "end_time" => $time]);
                }
            }
        }
        $res = ["code" => 0, "msg" => "成功"];
        echo json_encode($res);
        exit;
    }
    public function valid()
    {
        if(!empty($_GET["echostr"])) {
            echo $_GET["echostr"] . "";
            exit;
        }
        $xml_str = file_get_contents("php://input");
        $xml = (array) simplexml_load_string($xml_str, "SimpleXMLElement", LIBXML_NOCDATA);
        if(!empty($xml["FromUserName"]) && !empty($xml["Event"]) && in_array($xml["Event"], ["subscribe", "SCAN"])) {
            $open_id = $xml["FromUserName"];
            $unionid = "";
            if(!empty($open_id)) {
                $wx_setting = new \longbingcore\wxcore\WxSetting($this->_uniacid);
                $access_token = $wx_setting->getGzhToken();
                $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . $open_id . "&lang=zh_CN";
                $user_info = @file_get_contents($url);
                if(!empty($user_info)) {
                    $user_info = json_decode($user_info, true);
                    $unionid = !empty($user_info["unionid"]) ? $user_info["unionid"] : "";
                }
            }
            $pid = $xml["Event"] == "subscribe" ? str_replace("qrscene_", "", $xml["EventKey"]) : $xml["EventKey"];
            if(!empty($pid)) {
                $pid_count = explode("_", $pid);
                if(1 < count($pid_count) && $pid_count[1] == "channel") {
                    $this->bindChannel($open_id, $pid_count[0], $unionid);
                } elseif(1 < count($pid_count) && $pid_count[1] == "chaqr") {
                    $is_qr = 1;
                    $this->bindChannelQr($open_id, $pid_count[0], $unionid);
                } else {
                    $this->bindPid($open_id, $pid, $unionid);
                }
            }
            if(!empty($is_qr)) {
                $this->sendRegisterChannelMsg($xml, $open_id, $pid_count[0]);
            }
            if($xml["Event"] == "subscribe") {
                $text = getConfigSetting($this->_uniacid, "wechat_reply_text");
                if(!empty($text)) {
                    $this->SRCnt($xml, $text);
                }
            }
        }
        echo "";
        exit;
    }
    public function sendRegisterChannelMsg($xml, $open_id, $qr_code)
    {
        $qr_model = new \app\massage\model\ChannelQr();
        $channel_model = new \app\massage\model\ChannelList();
        $user_model = new \app\massage\model\User();
        $qr = $qr_model->dataInfo(["code" => $qr_code, "status" => 1]);
        if(empty($qr)) {
            return false;
        }
        if(!empty($qr["channel_id"])) {
            $channel = $channel_model->where(["id" => $qr["channel_id"]])->where("status", ">", -1)->find();
            if(!empty($channel)) {
                return false;
            }
            $qr_model->dataUpdate(["id" => $qr["id"]], ["channel_id" => 0]);
        }
        $user = $user_model->dataInfo(["openid" => $open_id, "status" => 1]);
        if(!empty($user)) {
            $channel = $channel_model->where(["user_id" => $user["id"]])->where("status", "in", [1, 2, 3])->find();
            if(!empty($channel)) {
                return false;
            }
        }
        $url = "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/user/pages/channel/apply?channel_qr_id=" . $qr["id"];
        $text = "<a href = \"" . $url . "\">请点击申请渠道商</a>";
        $this->SRCnt($xml, $text);
        return true;
    }
    public function bindPid($open_id, $pid, $unionid = "")
    {
        $pid = str_replace("d_", "", $pid);
        $db_code_do = getConfigSetting($this->_uniacid, "db_code_do");
        if($db_code_do == 1) {
            $count_data = explode(",", $pid);
            if(count($count_data) < 2) {
                return true;
            }
            $pid = $count_data[0];
        }
        $user_model = new \app\massage\model\User();
        $user = $user_model->dataInfo(["web_openid" => $open_id, "status" => 1]);
        $bind_model = new \app\massage\model\QrBind();
        $dis = ["openid" => $open_id];
        $find = $bind_model->dataInfo($dis);
        $fx_time_type = getConfigSetting($this->_uniacid, "fx_time_type");
        $fx_time_day = getConfigSetting($this->_uniacid, "fx_time_day");
        $insert = ["uniacid" => $this->_uniacid, "openid" => $open_id, "pid" => $pid, "create_time" => time(), "over_time" => time() + $fx_time_day * 86400, "forever" => $fx_time_type == 1 ? 0 : 1, "unionid" => $unionid];
        if(!empty($find)) {
            $bind_model->dataUpdate(["id" => $find["id"]], $insert);
        } else {
            $bind_model->insert($insert);
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => !empty($user["id"]) ? $user["id"] : 0, "qr_id" => $pid, "type" => 2, "open_id" => $open_id, "is_new" => !empty($user) ? 0 : 2];
        $scan_model = new \app\massage\model\ChannelScanQr();
        $scan_model->dataAdd($insert);
        return true;
    }
    public function bindChannelQr($open_id, $qr_code, $unionid = "")
    {
        $channel_model = new \app\massage\model\UserChannel();
        $user_model = new \app\massage\model\User();
        $user = $user_model->dataInfo(["web_openid" => $open_id, "status" => 1]);
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $this->_uniacid, ["channelforeverbind"]);
        $channel_bind_type = getConfigSetting((int) $this->_uniacid, "channel_bind_type");
        $channel_bind_forever = $auth["channelforeverbind"];
        if(!empty($user)) {
            $dis["user_id"] = $user["id"];
        }
        $dis["open_id"] = $open_id;
        if($channel_bind_type == 1) {
            if($channel_bind_forever) {
                $find = $channel_model->whereOr($dis)->find();
                $time = strtotime(date("Y-m-d", time()));
                if(!empty($user) && $user["create_time"] < $time) {
                    $find = 1;
                }
            } else {
                $channel_model->whereOr($dis)->delete();
            }
        } elseif($channel_bind_forever) {
            $find = $channel_model->whereOr($dis)->find();
        } else {
            $find = $channel_model->where("over_time", ">", time())->where(function ($query) {
                $query->whereOr($dis);
            })->find();
            if(empty($find)) {
                $channel_model->whereOr($dis)->delete();
            }
        }
        $user_channel_over_time = getConfigSetting($this->_uniacid, "user_channel_over_time");
        $qr_model = new \app\massage\model\ChannelQr();
        $qr = $qr_model->dataInfo(["code" => $qr_code, "status" => 1]);
        if(!empty($qr["channel_id"])) {
            $channels_model = new \app\massage\model\ChannelList();
            $channel = $channels_model->where(["id" => $qr["channel_id"]])->where("status", "in", [2, 3])->find();
            if(!empty($channel["time_type"]) && $channel["time_type"] == 1) {
                $user_channel_over_time = $channel["time"];
            }
        }
        if(!empty($qr)) {
            if(empty($find)) {
                $insert = ["uniacid" => $this->_uniacid, "user_id" => !empty($user["id"]) ? $user["id"] : 0, "open_id" => $open_id, "channel_id" => $qr["channel_id"], "channel_qr_id" => $qr["id"], "channel_qr_code" => $qr["code"], "unionid" => $unionid, "over_time" => time() + $user_channel_over_time * 3600];
                $channel_model->dataAdd($insert);
            }
            $insert = ["uniacid" => $this->_uniacid, "user_id" => !empty($user["id"]) ? $user["id"] : 0, "qr_id" => $qr["id"], "qr_code" => $qr["code"], "city" => $qr["city"], "province" => $qr["province"], "open_id" => $open_id, "is_new" => !empty($user) ? 0 : 2];
            $scan_model = new \app\massage\model\ChannelScanQr();
            $scan_model->dataAdd($insert);
        }
        return true;
    }
    public function bindChannel($open_id, $channel_id, $unionid = "")
    {
        $channel_model = new \app\massage\model\UserChannel();
        $user_model = new \app\massage\model\User();
        $user = $user_model->dataInfo(["web_openid" => $open_id, "status" => 1]);
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $this->_uniacid, ["channelforeverbind"]);
        $channel_bind_forever = $auth["channelforeverbind"];
        if(!empty($user)) {
            $dis["user_id"] = $user["id"];
        }
        $dis["open_id"] = $open_id;
        $channel_bind_type = getConfigSetting((int) $this->_uniacid, "channel_bind_type");
        if($channel_bind_type == 1) {
            if($channel_bind_forever) {
                $find = $channel_model->whereOr($dis)->find();
                $time = strtotime(date("Y-m-d", time()));
                if(!empty($user) && $user["create_time"] < $time) {
                    $find = 1;
                }
            } else {
                $channel_model->whereOr($dis)->delete();
            }
        } elseif($channel_bind_forever) {
            $find = $channel_model->whereOr($dis)->find();
        } else {
            $find = $channel_model->where("over_time", ">", time())->where(function ($query) {
                $query->whereOr($dis);
            })->find();
            if(empty($find)) {
                $channel_model->whereOr($dis)->delete();
            }
        }
        $user_channel_over_time = getConfigSetting($this->_uniacid, "user_channel_over_time");
        if(!empty($channel_id)) {
            $channels_model = new \app\massage\model\ChannelList();
            $channel = $channels_model->where(["id" => $channel_id])->where("status", "in", [2, 3])->find();
            if(!empty($channel["time_type"]) && $channel["time_type"] == 1) {
                $user_channel_over_time = $channel["time"];
            }
        }
        if(empty($find)) {
            $insert = ["uniacid" => $this->_uniacid, "user_id" => !empty($user["id"]) ? $user["id"] : 0, "open_id" => $open_id, "channel_id" => $channel_id, "over_time" => time() + $user_channel_over_time * 3600, "unionid" => $unionid];
            $res = $channel_model->dataAdd($insert);
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => !empty($user["id"]) ? $user["id"] : 0, "qr_id" => $channel_id, "type" => 9, "open_id" => $open_id, "is_new" => !empty($user) ? 0 : 2];
        $scan_model = new \app\massage\model\ChannelScanQr();
        $scan_model->dataAdd($insert);
        return true;
    }
    public function SRCnt($postObj, $text)
    {
        $toUser = $postObj["FromUserName"];
        $fromUser = $postObj["ToUserName"];
        $time = time();
        $msgType = "text";
        $content = $text;
        $template = "<xml>\n\n        <ToUserName><![CDATA[%s]]></ToUserName>\n\n        <FromUserName><![CDATA[%s]]></FromUserName>\n\n        <CreateTime>%s</CreateTime>\n\n        <MsgType><![CDATA[%s]]></MsgType>\n\n        <Content><![CDATA[%s]]></Content>\n\n        </xml>";
        $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
        echo $info;
    }
    public function getMapCoach()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", 666];
        $dis[] = ["status", "=", 2];
        if(!empty($input["city_id"])) {
            $dis[] = ["city_id", "=", $input["city_id"]];
        }
        $coach_model = new \app\massage\model\Coach();
        $data = $coach_model->where($dis)->field("id as coach_id,lng,lat,coach_name,work_img")->select()->toArray();
        return $this->success($data);
    }
}

?>